﻿using System;
using PersonaLibrary.Data;

namespace PersonaLibrary.ViewModel
{
    public abstract class EditableItemViewModel<T, TView> : BaseViewModel<TView>, ICommitCommand
        where TView : View.IView, new()
    {
        private RelayCommand _commitCommand;
		
        public event Action Commited;

        public RelayCommand CommitCommand
        {
            get
            {
                if (_commitCommand == null)
                    _commitCommand = new RelayCommand(param => Commit(), param => CanCommit());
                return _commitCommand;
            }
        }

        public T EditableItem { protected set; get; }

        public virtual void Commit()
        {
            
            if (Commited != null) 
				Commited();
            View.Close();
        }

        public virtual bool CanCommit()
        {
            return true;
        }
    }
}
